home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / newmat03.lha / newmat03 / submat.cxx < prev    next >
C/C++ Source or Header  |  1993-08-08  |  5KB  |  148 lines

  1. //$$ submat.cxx                         submatrices
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #include "include.hxx"
  6.  
  7. #include "newmat.hxx"
  8. #include "newmatrc.hxx"
  9.  
  10.  
  11. //#define REPORT { static ExeCounter ExeCount(__LINE__,6); ExeCount++; }
  12.  
  13. #define REPORT {}
  14.  
  15.  
  16. /****************************** submatrices *********************************/
  17.  
  18. GetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  19.    int last_col)
  20. {
  21.    REPORT
  22.    int a = first_row - 1; int b = last_row - first_row + 1;
  23.    int c = first_col - 1; int d = last_col - first_col + 1;
  24.    if (a<0 || b<=0 || c<0 || d<=0) MatrixError("SubMatrix dimension error");
  25.    return GetSubMatrix(this, a, b, c, d, Type().sub());
  26. }
  27.  
  28. GetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row)
  29. {
  30.    REPORT
  31.    int a = first_row - 1; int b = last_row - first_row + 1;
  32.    if (a<0 || b<=0) MatrixError("SubMatrix dimension error");
  33.    return GetSubMatrix( this, a, b, a, b, Type().ssub());
  34. }
  35.  
  36. GetSubMatrix BaseMatrix::Row(int first_row)
  37. {
  38.    REPORT
  39.    int a = first_row - 1;
  40.    if (a<0) MatrixError("SubMatrix dimension error");
  41.    return GetSubMatrix(this, a, 1, 0, NcolsV(), MatrixType::RowV);
  42. }
  43.  
  44. GetSubMatrix BaseMatrix::Rows(int first_row, int last_row)
  45. {
  46.    REPORT
  47.    int a = first_row - 1; int b = last_row - first_row + 1;
  48.    if (a<0 || b<=0) MatrixError("SubMatrix dimension error");
  49.    return GetSubMatrix(this, a, b, 0, NcolsV(), Type().sub());
  50. }
  51.  
  52. GetSubMatrix BaseMatrix::Column(int first_col)
  53. {
  54.    REPORT
  55.    int c = first_col - 1;
  56.    if (c<0) MatrixError("SubMatrix dimension error");
  57.    return GetSubMatrix(this, 0, NrowsV(), c, 1, MatrixType::ColV);
  58. }
  59.  
  60. GetSubMatrix BaseMatrix::Columns(int first_col, int last_col)
  61. {
  62.    REPORT
  63.    int c = first_col - 1; int d = last_col - first_col + 1;
  64.    if (c<0 || d<=0) MatrixError("SubMatrix dimension error");
  65.    return GetSubMatrix(this, 0, NrowsV(), c, d, Type().sub());
  66. }
  67.  
  68. void GetSubMatrix::operator<<(BaseMatrix& bmx)
  69. {
  70.    REPORT
  71.    GeneralMatrix* gmx = bmx.Evaluate();
  72.    GeneralMatrix* gm = bm->Evaluate();
  73.    if ((BaseMatrix*)gm!=bm) MatrixError("Illegal argument for SubMatrix");
  74.    if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  75.       MatrixError("Dimensions don't match");
  76.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  77.       MatrixError("SubMatrix dimension error");
  78.    int i = row_number;
  79.    MatrixRow mrx(gmx, LoadOnEntry); 
  80.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  81.                                   // do need LoadOnEntry
  82.    MatrixRowCol sub;
  83.    while (i--)
  84.    {
  85.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  86.       sub.Copy(mrx); mr.Next(); mrx.Next();
  87.    }
  88.    gmx->tDelete();
  89. }   
  90.  
  91. void GetSubMatrix::operator<<(const real* r)
  92. {
  93.    REPORT
  94.    GeneralMatrix* gm = bm->Evaluate();
  95.    if ((BaseMatrix*)gm!=bm) MatrixError("Illegal argument for SubMatrix");
  96.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  97.       MatrixError("SubMatrix dimension error");
  98.    int i = row_number;
  99.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  100.                                   // do need LoadOnEntry
  101.    MatrixRowCol sub;
  102.    while (i--)
  103.    {
  104.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  105.       sub.Copy(r); mr.Next();
  106.    }
  107. }   
  108.  
  109. void GetSubMatrix::operator<<(real r)
  110. {
  111.    REPORT
  112.    GeneralMatrix* gm = bm->Evaluate();
  113.    if ((BaseMatrix*)gm!=bm) MatrixError("Illegal argument for SubMatrix");
  114.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  115.       MatrixError("SubMatrix dimension error");
  116.    int i = row_number;
  117.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  118.                                   // do need LoadOnEntry
  119.    MatrixRowCol sub;
  120.    while (i--)
  121.    {
  122.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  123.       sub.Copy(r); mr.Next();
  124.    }
  125. }   
  126.  
  127. void GetSubMatrix::Inject(const GeneralMatrix& gmx)
  128. {
  129.    REPORT
  130.    GeneralMatrix* gm = bm->Evaluate();
  131.    if ((BaseMatrix*)gm!=bm) MatrixError("Illegal argument for SubMatrix");
  132.    if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
  133.       MatrixError("Dimensions don't match");
  134.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  135.       MatrixError("SubMatrix dimension error");
  136.    int i = row_number;
  137.    MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry);
  138.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  139.                                   // do need LoadOnEntry
  140.    MatrixRowCol sub;
  141.    while (i--)
  142.    {
  143.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  144.       sub.Inject(mrx); mr.Next(); mrx.Next();
  145.    }
  146. }
  147.  
  148.